﻿using BCCommon;
using BCCommon.Enums;
using BCDto.WeChatComponent.WeChatMediaRecord;
using BCService.WeChatComponent.WeChatMaterialService.WeChatMaterialService;
using BCService.WeChatComponent.WeChatMediaRecordService;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BCTMSRESTful.Controllers.Management.WeChat
{
    /// <summary>
    /// 微信素材控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class WeChatMaterialController : ManagementBaseController
    {
        private readonly IWeChatMediaRecordService weChatMediaRecordService;
        private readonly IWeChatMaterialService weChatMaterialService;
        public WeChatMaterialController(IWeChatMediaRecordService weChatMediaRecordService,
            IWeChatMaterialService weChatMaterialService)
        {
            this.weChatMediaRecordService = weChatMediaRecordService;
            this.weChatMaterialService = weChatMaterialService;
        }
        /// <summary>
        /// 获取微信素材信息列表（带分页）。
        /// </summary>
        /// <param name="mediaId">素材Id</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="type">素材类型</param>
        /// <param name="isTemporary">临时素材</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">获取的微信素材信息列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<WeChatMediaRecordDto>), 200)]
        public IActionResult Get([FromQuery(Name = "media_id")]string mediaId,
            [FromQuery(Name = "file_name")]string fileName,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "material_type")]WeChatMediaType? type,
            [FromQuery(Name = "is_temporary")]bool? isTemporary,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.weChatMediaRecordService.GetRecords(mediaId,fileName, start, end, type.HasValue?type.ToString():string.Empty, isTemporary, ascending, index, size));
        }

        /// <summary>
        /// 获取本地微信素材信息。
        /// </summary>
        /// <param name="mediaId">素材Id</param>
        /// <response code="200">获取本地微信素材信息。</response>
        /// <returns></returns>
        [HttpGet("{media_id}")]
        [ProducesResponseType(typeof(WeChatMediaRecordDto), 200)]
        public IActionResult Get([FromRoute(Name ="media_id")]string mediaId)
        {
            return this.Ok(this.weChatMediaRecordService.GetEntity(mediaId));
        }
        /// <summary>
        /// 获取本地微信素材总数。
        /// </summary>
        /// <response code="200">获取本地微信素材总数。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(WeChatMediaRecordCountDto), 200)]
        public IActionResult Count()
        {
            return this.Ok(this.weChatMediaRecordService.Count());
        }
        /// <summary>
        /// 删除微信素材。
        /// </summary>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{media_id}")]
        public IActionResult Delete([FromRoute(Name ="media_id")]string mediaId)
        {
            this.weChatMediaRecordService.Delete(mediaId);
            return this.NoContent();
        }
        /// <summary>
        /// 删除微信素材（批量）。
        /// </summary>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Delete([FromQuery(Name = "media_ids")]string[] mediaIds)
        {
            this.weChatMediaRecordService.Delete(mediaIds);
            return this.NoContent();
        }
        /// <summary>
        /// 同步微信永久素材到本地
        /// </summary>
        /// <response code="200"></response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult SynchronizeWeChatPermanentMaterial()
        {
            this.weChatMediaRecordService.SynchronizeWeChatPermanentMaterial();

            return this.Ok(true);
        }
        /// <summary>
        /// 重传本地临时素材到微信临时素材
        /// </summary>
        /// <response code="200"></response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult UploadTemporaryFileToWeChat()
        {
            throw new NotImplementedException();
            
            //this.weChatMaterialService.UploadTemporaryFileToWeChat();
            //return this.Ok(true);
        }
        
    }
}
